<html>
<head><meta charset="utf-8"><title>does rust have a #[used] attribute for fns? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html">does rust have a #[used] attribute for fns?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222485481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222485481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222485481">(Jan 12 2021 at 19:54)</a>:</h4>
<p>In clang I can annotate a function with <code>__attribute__((used))</code> in order to prevent the linker from removing it in the object file, even if it's unused. I notice there is a <code>#[used]</code> attribute in Rust, but it only works for statics. Is there a similar attribute for functions too?</p>



<a name="222485662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222485662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222485662">(Jan 12 2021 at 19:55)</a>:</h4>
<p><code>#[used]</code> doesn't influence the linker</p>



<a name="222485810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222485810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222485810">(Jan 12 2021 at 19:56)</a>:</h4>
<p><code>#[no_mangle]</code> should do the trick though?</p>



<a name="222486047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222486047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222486047">(Jan 12 2021 at 19:58)</a>:</h4>
<p>Ah ok, I'll give it a try. I wasn't aware <code>#[no_mangle]</code> had additional semantics to simply preventing name mangling.</p>



<a name="222487114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222487114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222487114">(Jan 12 2021 at 20:06)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span>  - that did the job. Thanks a bunch!</p>



<a name="222490663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222490663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222490663">(Jan 12 2021 at 20:37)</a>:</h4>
<p>I think <code>#[no_mangle]</code> kind of means "leave this function's API alone".</p>



<a name="222490839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222490839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222490839">(Jan 12 2021 at 20:38)</a>:</h4>
<p>It does not affect the ABI, if you meant that</p>



<a name="222490883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222490883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222490883">(Jan 12 2021 at 20:38)</a>:</h4>
<p>It disables name mangling and publicly exports the function in the resulting object file</p>



<a name="222490938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222490938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222490938">(Jan 12 2021 at 20:39)</a>:</h4>
<p>Yeah, it's hard to phrase in a clear way <span aria-label="slight frown" class="emoji emoji-1f641" role="img" title="slight frown">:slight_frown:</span></p>



<a name="222491013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222491013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222491013">(Jan 12 2021 at 20:39)</a>:</h4>
<p>So maybe it's like no name mangling + C <code>static</code> function?</p>



<a name="222491116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222491116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222491116">(Jan 12 2021 at 20:40)</a>:</h4>
<p><code>static</code> on a C function does the opposite</p>



<a name="222491132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222491132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222491132">(Jan 12 2021 at 20:40)</a>:</h4>
<p>it makes it local to the compilation unit</p>



<a name="222491378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/does%20rust%20have%20a%20%23%5Bused%5D%20attribute%20for%20fns%3F/near/222491378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/does.20rust.20have.20a.20.23.5Bused.5D.20attribute.20for.20fns.3F.html#222491378">(Jan 12 2021 at 20:42)</a>:</h4>
<p>Oops, yeah I guess I meant the opposite of <code>static</code> <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>